package com.sky.controller;

import com.sky.result.Result;
import com.sky.utils.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping()
@Api(tags = "文件上传接口")
@Slf4j
public class FileUploadController {

    @Value("${file.upload.appeal}")
    private String appealUploadPath;
    @Value("${file.upload.execution}")
    private String executionUploadPath;

    @PostMapping("/upload/appeal")
    @ApiOperation("上传申诉材料")
    public Result<String> uploadAppealMaterial(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            log.warn("文件为空，未上传任何内容");
            return Result.error("文件为空，请选择要上传的文件");
        }

        try {
            log.info("开始上传文件: {}", file.getOriginalFilename());

            // 使用时间戳重命名文件
            String fileName = FileUploadUtils.getFileNameWithTimestamp(file.getOriginalFilename());

            // 保存文件
            FileUploadUtils.writeBytesToFile(file.getBytes(), appealUploadPath, fileName);

            log.info("文件上传成功: {} -> {}/{}", file.getOriginalFilename(), appealUploadPath, fileName);

            // 返回相对路径供前端访问（去掉D盘路径前缀）
            String relativePath = "/images/appeal/" + fileName;
            return Result.success(relativePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e.getMessage(), e); // 输出异常堆栈
            return Result.error("文件上传失败：" + e.getMessage());
        }
    }

    @PostMapping("/upload/execution")
    @ApiOperation("上传执法材料")
    public Result<String> uploaExecutionMaterial(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            log.warn("文件为空，未上传任何内容");
            return Result.error("文件为空，请选择要上传的文件");
        }

        try {
            log.info("开始上传文件: {}", file.getOriginalFilename());

            // 使用时间戳重命名文件
            String fileName = FileUploadUtils.getFileNameWithTimestamp(file.getOriginalFilename());

            // 保存文件
            FileUploadUtils.writeBytesToFile(file.getBytes(), executionUploadPath, fileName);

            log.info("文件上传成功: {} -> {}/{}", file.getOriginalFilename(), executionUploadPath, fileName);

            // 返回相对路径供前端访问（去掉D盘路径前缀）
            String relativePath = "/images/execution/" + fileName;
            return Result.success(relativePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e.getMessage(), e); // 输出异常堆栈
            return Result.error("文件上传失败：" + e.getMessage());
        }
    }

}
